/*
 * McCullochPittsNeuron.cpp
 *
 *  Created on: Sep 5, 2013
 *      Author: filipe
 */
#include <cstdio>
#include <cstdlib>
#include "Activation.h"
#include "McCullochPittsNeuron.h"


McCullochPittsNeuron::McCullochPittsNeuron() : Neuron()
{
	_num_synapsis = 0;
	_synapsis_weight = NULL;
	_activation_function = NULL;
}


McCullochPittsNeuron::McCullochPittsNeuron(int num_synapsis, ActivationFunction *activation_function) : Neuron()
{
	_num_synapsis = num_synapsis;
	_synapsis_weight = (double *) calloc (num_synapsis, sizeof(double));
	_activation_function = activation_function;
}


McCullochPittsNeuron::~McCullochPittsNeuron()
{
	if (_synapsis_weight)
		free(_synapsis_weight);

	if (_activation_function)
		delete(_activation_function);
}


void
McCullochPittsNeuron::SetNumSynapsis(int num_synapsis)
{
	_num_synapsis = num_synapsis;

	if (_synapsis_weight)
		free(_synapsis_weight);

	_synapsis_weight = (double *) calloc (num_synapsis, sizeof(double));
}


void
McCullochPittsNeuron::SetActivationFunction(ActivationFunction *activation_function)
{
	if (_activation_function)
		delete(_activation_function);

	_activation_function = activation_function;
}


int
McCullochPittsNeuron::GetNumSynapsis(void) const
{
	return _num_synapsis;
}


ActivationFunction*
McCullochPittsNeuron::GetActivationFunction(void) const
{
	return _activation_function;
}


void
McCullochPittsNeuron::SetSynapticWeight(int synapsis, double value)
{
	if ((synapsis >= _num_synapsis) || (_synapsis_weight == NULL))
		exit(printf("Error: Invalid synaptic index: %d when only %d synapsis are alloc'd\n", synapsis, _num_synapsis));

	_synapsis_weight[synapsis] = value;
}


double
McCullochPittsNeuron::Evaluate(double *input) const
{
	int i;
	double neuron_total_load;
	double neuron_activation;

	neuron_total_load = 0;

	for (i = 0; i < _num_synapsis; i++)
		neuron_total_load += (input[i] * _synapsis_weight[i]);

	neuron_activation = _activation_function->Evaluate(neuron_total_load);
	return neuron_activation;
}



